package composite;

import factory.A;

import java.util.ArrayList;

abstract class Node{
    public abstract void p();
}

// 节点分 叶子节点和非叶子节点

// 叶子节点
class LeafNode extends Node{
    private String name;

    public LeafNode(String name){
        this.name = name;
    }

    @Override
    public void p() {
        System.out.println(name);
    }
}

class BranchNode extends Node{
    private String name;
    private ArrayList<Node> list = new ArrayList<>();

    public BranchNode(String name){
        this.name = name;
    }

    public BranchNode add(Node node){
        list.add(node);
        return this;
    }

    public void p(){
        System.out.println(name);
    }

    public ArrayList<Node> getList(){
        return list;
    }
}


/**
 * 组合设计模式专门负责处理树的
 */
public class Call {
    public static void main(String[] args){
        // System.out.println("print main");

        // 创建跟节点
        BranchNode root = new BranchNode("root");
        // 创建第二层目录
        BranchNode dic1_1 = new BranchNode("dic1_1");
        BranchNode dic1_2 = new BranchNode("dic1_2");
        // 第三层目录
        BranchNode dic2_1 = new BranchNode("dic2_1");
        // 创建第三层叶子
        Node leaf1_1_1 = new LeafNode("leaf1_1_1");
        Node leaf1_1_2 = new LeafNode("leaf1_1_2");

        Node leaf1_2_1 = new LeafNode("leaf1_2_1");
        Node leaf1_2_2 = new LeafNode("leaf1_2_2");

        // 构建层级
        root.add(dic1_1).add(dic1_2);
        dic1_1.add(leaf1_1_1).add(leaf1_1_2);
        dic1_2.add(dic2_1).add(leaf1_2_1).add(leaf1_2_2);

        Call.print(root, 0);
    }
    public static void print(Node p, int deep){
        for(int i=0; i< deep; i++){
            System.out.print("--");
        }
        p.p();

        if(p instanceof BranchNode){
            for(Node item:((BranchNode) p).getList()){
                Call.print(item,deep+1);
            }
        }
    }
}
